import random
import requests
import time
from get_anti_content import get_anti_content, get_proxy_no, get_access_token

cookies = {
    '_bee': 'j4sFD92EXhkNW1g6CBXTtVFhzZfFD0yO',
    '_f77': 'eff1e851-acec-4086-a75c-8103cb0a5907',
    '_a42': 'be292171-864e-45ac-a08b-68971f365e12',
    'rckk': 'j4sFD92EXhkNW1g6CBXTtVFhzZfFD0yO',
    'ru1k': 'eff1e851-acec-4086-a75c-8103cb0a5907',
    'ru2k': 'be292171-864e-45ac-a08b-68971f365e12',
    'JSESSIONID': '1C3FEBF2259448375A78D6BC542811C3',
    'api_uid': 'CiYY3mY83oxmEABoQxZcAg==',
}

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
    'accesstoken': 'FFI246EXOUGFUKX25C3RGY7SEEHO3GMR6FIK7PVXPSBHMFBWRWJA1200774',
    'anti-content': '0asWfqnyXjQgjgT9Q0hilGnWQl57wXQfA6AmEWrm_dFjgG9s9xtpmAuMHvALAGAocj84_yM12_DsZlyeZTzbiAfPYBMtFuf6Qp5iydU2RPnQvUdFaCj0CdQXlNAC1SXY6OYaGZuuBJE9Re4JGRRr6Di8ncqSbEnBhdJixSbjden3homIXtnnMkZIeyk7BhU9ZzmMPecwPh5lHuAtuQ3qxpHclK8bZScNaZx57PsNTdIN2Dx2SRe0uTqYX97b5ksRT1sVLt2MPnkhPhKTZ7D5niX6vewQ3-3WPzek2HaBm6lGX2JxBOLehdIoFLxV6ZXuTCxFttf7tfb5u2iCgEODi6CSjhF7HdzjeGrXb3UsTrxCnkxlgoQ1uufovqtMQaKPW-4yc9GUho3IRm1roGlxKrRQEPKfgFR_Trbf_FVZ93L93fQN9Iq62wiVD6q_Huq-uYNdgcLq7Dtkf2XLKDZNS3le0jx813Xsn5UXAeHugPS_alqjXF7kDM00vh7Ku7jFj6AbCj5V2n-jZQcUayl6B9nQDhxCvKI_YHe_4kQpPJJ5ZPSbBB6jvnIEoiEdoeK-IFDVMPHzF5fMZ5Z1fF8fKkcJ3ZTSX1D8oUxqE5CkcdxXLsefNkqzIBgeOAtlkfXBMDbfqRKPLEXFdfoQn8U0d_0UCiDyDJAgp8wgXjgd4wsOYANM2aHUIp4KjjAi3bNk_bhp-CCoEsie0e9zVG32hrCBZE7Yaz',
    'content-type': 'application/json;charset=UTF-8',
    'origin': 'https://ktt.pinduoduo.com',
    'priority': 'u=1, i',
    'referer': 'https://ktt.pinduoduo.com/',
    'sec-ch-ua': '"Chromium";v="134", "Not:A-Brand";v="24", "Google Chrome";v="134"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36',
    # 'cookie': '_bee=j4sFD92EXhkNW1g6CBXTtVFhzZfFD0yO; _f77=eff1e851-acec-4086-a75c-8103cb0a5907; _a42=be292171-864e-45ac-a08b-68971f365e12; rckk=j4sFD92EXhkNW1g6CBXTtVFhzZfFD0yO; ru1k=eff1e851-acec-4086-a75c-8103cb0a5907; ru2k=be292171-864e-45ac-a08b-68971f365e12; JSESSIONID=1C3FEBF2259448375A78D6BC542811C3; api_uid=CiYY3mY83oxmEABoQxZcAg==',
}

def query_category():
    params = {
        'sign': ''.join(random.choice('0123456789ABCDEF') for _ in range(32)),
        'signVersion': 'sv1',
        'timestamp': int(time.time() * 1000),
        'proxy_no': get_proxy_no(),
    }
    
    print('GET', 'https://api.pinduoduo.com/api/ktt_group/goods_library/category/query')
    response = requests.get(
        'https://api.pinduoduo.com/api/ktt_group/goods_library/category/query',
        params=params,
        cookies=cookies,
        headers=headers,
    )
    if  response.status_code != 200:
        print('请求失败', response.status_code, response.text)
    else:
        # print('请求成功', response.json())
        pass
    return response.json()

if __name__ == '__main__':
    headers['accesstoken'] = get_access_token()
    headers['anti-content'] = get_anti_content()

    response = query_category()
    # "result": {
    #     "list": [
    #         {
    #             "category_id": 23164346,
    #             "category_name": "大童羽绒-24秋冬",
    #             "priory": 141,
    #             "is_sys_default": false,
    #             "goods_material_count": 0
    #         },
    #         {
    #             "category_id": 17703824,
    #             "category_name": "英氏高端系列",
    #             "priory": 140,
    #             "is_sys_default": false,
    #             "goods_material_count": 0
    #         }
    #     ]
    # }
    if response.get('error_code') == 1000000:
        for category in response.get('result').get('list'):
            print(f"{category.get('category_name')[:30]:<30}{category.get('category_id')}")

    input("按任意键退出...")
